package cn.hjm.hrm.web.controller;

import cn.hjm.hrm.dto.ProductDto;
import cn.hjm.hrm.service.IOrderService;
import cn.hjm.hrm.domain.Order;
import cn.hjm.hrm.query.OrderQuery;
import cn.hjm.util.AjaxResult;
import cn.hjm.util.CodeGenerateUtils;
import cn.hjm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    public IOrderService orderService;

    @PostMapping("/createorder")
    public Order createorder(@RequestBody ProductDto productDto) {
        Order order = new Order();
        order.setUserId(2L);
        order.setName(productDto.getName());
        order.setNumber(productDto.getNumber());
        order.setPrice(productDto.getPrice());
        order.setTotalPrice(productDto.getPrice().multiply(new BigDecimal(productDto.getNumber())));
        order.setOrderNo(CodeGenerateUtils.generateOrderSn(order.getUserId()));
        orderService.saveOrder(order);
        return order;
    }
    /**
    * 保存和修改公用的
    * @param order  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Order order){
        try {
            if(order.getId()!=null){
                orderService.updateById(order);
            }else{
                orderService.insert(order);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Order get(@PathVariable("id")Long id)
    {
        return orderService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Order> list(){

        return orderService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Order> json(@RequestBody OrderQuery query)
    {
        Page<Order> page = new Page<Order>(query.getPage(),query.getRows());
        page = orderService.selectPage(page);
        return new PageList<Order>(page.getTotal(),page.getRecords());
    }
}
